const { pool, testConnection } = require('./db');

async function testDatabase() {
  try {
    console.log('开始测试数据库连接...');
    
    // 测试连接
    await testConnection();
    
    // 测试查询用户表
    const [users] = await pool.execute('SELECT COUNT(*) as count FROM users');
    console.log(`用户表中有 ${users[0].count} 条记录`);
    
    // 测试查询样品表
    const [samples] = await pool.execute('SELECT COUNT(*) as count FROM samples');
    console.log(`样品表中有 ${samples[0].count} 条记录`);
    
    // 测试用户登录
    const crypto = require('crypto');
    const testPassword = crypto.createHash('sha256').update('123456').digest('hex');
    
    const [loginResult] = await pool.execute(
      'SELECT id, username, name, role FROM users WHERE username = ? AND password = ?',
      ['admin', testPassword]
    );
    
    if (loginResult.length > 0) {
      console.log('登录测试成功:', loginResult[0]);
    } else {
      console.log('登录测试失败');
    }
    
    console.log('数据库测试完成！');
    
  } catch (error) {
    console.error('数据库测试失败:', error);
  } finally {
    await pool.end();
  }
}

testDatabase(); 